package viewModel;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;

import Controller.UserCredentialManager;
import Misc.Variables;

import databaseHelper.ExamDAO;
import datatypes.Exam;

/**
 * This is the ViewModel for the dashboard
 * 
 * accesses the database for variables needed
 * 
 * 
 * 
 * @author Rupert
 *
 */
public class ExamViewModel {

	/**
	 * Data Access Object
	 */
	private ExamDAO exams = new ExamDAO();
	
	/**
	 * selected exam in listbox
	 */
	private Exam selectedExam;
		
	/**
	 * List of all exams of a user used for listbox
	 */
	private List<Exam> examList = new ArrayList<Exam>();
		
	/**
	 * statistics on the exams
	 */
	private int nbrOfExams = 0;
	private int nbrOfExamsInPast = 0;
	private int nbrOfExamsCurrently = 0;
	private int nbrOfExamsFuture = 0;

	@Init
    public void init(){
		/*
		 * if user is logged in, the exams and statistics are loaded
		 */
		UserCredentialManager user = (UserCredentialManager) Sessions.getCurrent().getAttribute(UserCredentialManager.KEY_USER_Model);
		if(user!= null){
			examList= exams.findAllofUser(user.getUserId());
			int past = 0;
			int present = 0;
			int future = 0;
			
			Date curDate = new Date();
			
			for(Exam exam : examList){
				if(curDate.before(exam.getEvaluationBeginDate()))
					future++;
				else if(curDate.after(exam.getEvaluationEndDate()))
						past++;
				else
					present++;
			}
			
			setNbrOfExamsInPast(past);
			setNbrOfExamsCurrently(present);
			setNbrOfExamsFuture(future);
//			setNbrOfExamsInPast(exams.getNumberOfExamsInPast(user.getUserId()));
//			setNbrOfExamsCurrently(exams.getNumberOfExamsCurrent(user.getUserId()));
//			setNbrOfExamsFuture(exams.getNumberOfExamsFuture(user.getUserId()));
			nbrOfExams = examList.size();
		}else{
			Executions.sendRedirect("login.zul");
		}		
    }
	
	public int getIdOfSelectedExam(){
		if(this.selectedExam != null){ 
			return this.selectedExam.getId();	
		}
		return -1;
	}
	
	public Exam getSelectedExam(){
		return selectedExam;
	}
	
	public void setSelectedExam(Exam selectedExam){
		this.selectedExam = selectedExam;
	}
	
	
	/**
	 * Commands & variables( getters and setters) for The View
	 */
	
	
	/**
	 * get a list of all exams the user has created!
	 * @return List<Exam>
	 */
	public List<Exam> getExams(){
		
		return examList;
	}
	
	@Command("watchExam")
	@NotifyChange("exams")
	public void watchExam(){
		/**
		 * open examViewer; adding a session attribute for the path
		 */
		Sessions.getCurrent().setAttribute(Variables.examPDFPathAttribut, this.selectedExam.getPdfPath());
		Executions.sendRedirect("examViewer.zul");
	}
	
	@Command("watchWorkedExams")
	@NotifyChange("exams")
	public void watchWorkedExams(){
		/**
		 * open evaluation statistic; adding a session attribute for the id of the selected exam
		 */
		Sessions.getCurrent().setAttribute(Variables.examPDFPathAttribut, this.selectedExam.getPdfPath());
		Sessions.getCurrent().setAttribute(Variables.workedExamSessionAttribut, this.selectedExam.getId());
		Executions.sendRedirect("workedExam.zul");
	}
	
	@Command("update")
	@NotifyChange({"exams", "selectedExam", "nbrOfExamsFuture", "nbrOfExamsCurrently", "nbrOfExamsInPast"})
	public void update(){
		
		/**
		 * updating the given values in the exam
		 */
		exams.update(this.selectedExam.getId(), this.selectedExam.getSubjectName(), this.selectedExam.getNewEvaluationBegin(), this.selectedExam.getNewEvaluationEnd(), this.selectedExam.getExamDate());

		Date curDate = new Date();
		
		if(curDate.before(selectedExam.getNewEvaluationBegin()))
			nbrOfExamsFuture++;
		else if(curDate.after(this.selectedExam.getNewEvaluationEnd()))
			nbrOfExamsInPast++;
		else
			nbrOfExamsCurrently++;
		
		if(curDate.before(selectedExam.getEvaluationBeginDate()))
			nbrOfExamsFuture--;
		else if(curDate.after(selectedExam.getEvaluationEndDate()))
			nbrOfExamsInPast--;
		else
			nbrOfExamsCurrently--;
		
		selectedExam.setEvaluationBeginDate(new Date(this.selectedExam.getNewEvaluationBegin().getTime()));
		selectedExam.setEvaluationEndDate(new Date(this.selectedExam.getNewEvaluationEnd().getTime()));
		
		
	}
	
	@Command("edit")
	@NotifyChange("exams")
	public void editExam(){
		/**
		 * open exam editor with attributes for composer and for page
		 */
		Sessions.getCurrent().setAttribute(Variables.editExamSessionAttribut, this.selectedExam.getId());
		Sessions.getCurrent().setAttribute(Variables.editExamComposerSessionAttribut, this.selectedExam.getId());
		Executions.sendRedirect("examEditor.zul");
	}
	
	@Command("delete")
	@NotifyChange({"exams", "selectedExam", "nbrOfExamsFuture", "nbrOfExamsCurrently", "nbrOfExamsInPast", "nbrOfExams"})
	public void delete() {

		/**
		 * deleting the curExam
		 */
		
		exams.delete(this.selectedExam);

		Date curDate = new Date();
		
		if(curDate.before(selectedExam.getEvaluationBeginDate()))
			nbrOfExamsFuture--;
		else if(curDate.after(selectedExam.getEvaluationEndDate()))
			nbrOfExamsInPast--;
		else
			nbrOfExamsCurrently--;
		
		nbrOfExams--;
		
		examList.remove(this.selectedExam);
		this.selectedExam = null;	
	}

	/**
	 * @return the nbrOfExercises
	 */
	public int getNbrOfExams() {
		return nbrOfExams;
	}

	/**
	 * @param nbrOfExercises the nbrOfExercises to set
	 */
	public void setNbrOfExams(int nbrOfExams) {
		this.nbrOfExams = nbrOfExams;
	}

	/**
	 * @return the nbrOfExamsInPast
	 */
	public int getNbrOfExamsInPast() {
		return nbrOfExamsInPast;
	}

	/**
	 * @param nbrOfExamsInPast the nbrOfExamsInPast to set
	 */
	public void setNbrOfExamsInPast(int nbrOfExamsInPast) {
		this.nbrOfExamsInPast = nbrOfExamsInPast;
	}

	/**
	 * @return the nbrOfExamsCurrently
	 */
	public int getNbrOfExamsCurrently() {
		return nbrOfExamsCurrently;
	}

	/**
	 * @param nbrOfExamsCurrently the nbrOfExamsCurrently to set
	 */
	public void setNbrOfExamsCurrently(int nbrOfExamsCurrently) {
		this.nbrOfExamsCurrently = nbrOfExamsCurrently;
	}

	/**
	 * @return the nbrOfExamsFuture
	 */
	public int getNbrOfExamsFuture() {
		return nbrOfExamsFuture;
	}

	/**
	 * @param nbrOfExamsFuture the nbrOfExamsFuture to set
	 */
	public void setNbrOfExamsFuture(int nbrOfExamsFuture) {
		this.nbrOfExamsFuture = nbrOfExamsFuture;
	}

	
	
	
}
